package com.apes.fn.tms.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.PhyDepot;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 运输表
 * @author yiny
 * 2020.3.9
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name=Waybill.CONST_TABLE_NAME)
public class Waybill extends Model {
    public static final String CONST_TABLE_NAME = "tms_waybill";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "运输单")
    @Comment("运单号")
    private String id;

    @Column(name = "source_business_type", length = 64)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_key", length = 64)
    @Comment("来源单据号码")
    private String sourceBusinessKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delivery_address", referencedColumnName = "id")
    @Comment("发货地址")
    private PhyDepot deliveryAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shipping_address", referencedColumnName = "id")
    @Comment("收货地址")
    private PhyDepot shippingAddress;

    @Column(name = "received_date")
    @Comment("签收时间")
    private Date receivedDate;

    @States(name="运输状态",
            values = {@State(id="collection", name="待揽收"), @State(id = "sendCar", name = "待派车"),@State(id="temporary", name="暂存中转站"),
                    @State(id="loading", name = "待装车"), @State(id="inTransit", name="运输中"), @State(id="received", name="已签收"),
            @State(id="abrogation", name = "终止")})
    @Column(name = "transport_status", length = 64)
    @Comment("运输状态")
    private String transportStatus;

    @OneToMany(mappedBy = "waybillId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<WaybillCargoDetails> waybillCargoDetailsSet = new ArrayList<>();

    public List<WaybillCargoDetails> getWaybillCargoDetailsSet() {
        if (waybillCargoDetailsSet.isEmpty() || waybillCargoDetailsSet == null ) return null;
        for (WaybillCargoDetails waybillCargoDetails:waybillCargoDetailsSet) {
            waybillCargoDetails.setWaybillId(this);
        }
        return waybillCargoDetailsSet;
    }
    public void setWaybillCargoDetailsSet(List<WaybillCargoDetails> waybillCargoDetailsSet) {
        if (waybillCargoDetailsSet == null) waybillCargoDetailsSet = new ArrayList<>();
        this.waybillCargoDetailsSet.clear();
        this.waybillCargoDetailsSet.addAll(waybillCargoDetailsSet);
    }

    @OneToMany(mappedBy = "waybillId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<WaybillRoutePlanning> waybillRoutePlanningSet = new ArrayList<>();
    public List<WaybillRoutePlanning> getWaybillRoutePlanningSet() {
        if (waybillRoutePlanningSet.isEmpty() || waybillRoutePlanningSet == null ) return null;
        for (WaybillRoutePlanning waybillRoutePlanning:waybillRoutePlanningSet) {
            waybillRoutePlanning.setWaybillId(this);
        }
        return waybillRoutePlanningSet;
    }

    public void setWaybillRoutePlanningSet(List<WaybillRoutePlanning> waybillRoutePlanningSet) {
        if (waybillRoutePlanningSet == null) waybillRoutePlanningSet = new ArrayList<>();
        this.waybillRoutePlanningSet.clear();
        this.waybillRoutePlanningSet.addAll(waybillRoutePlanningSet);
    }

    @Column(name = "loading_time")
    @Comment("装货时间")
    private Date loadingTime; // 整张运单最开始装货的时间

    @Column(name = "execution_number",columnDefinition = "number(10) default 0")
    @Comment("当前步数")
    private int executionNumber ; // 运单当前执行的方案的当前步数

    @Column(name = "total_number",columnDefinition = "number(10) default 0")
    @Comment("总步数")
    private int TotalNumber; // 当前执行的方案的总步数

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site", referencedColumnName = "id")
    @Comment("暂存站点")
    private Site site;  // 暂存站点

    @Column(name = "transit_date")
    @Comment("暂存收件时间")
    private Date transitDate;

    @Column(name = "outbound_Order", length = 64)
    @Comment("出库单号")
    private String outboundOrder;

    @Column(name = "receipt_Order", length = 64)
    @Comment("入库单号")
    private String receiptOrder;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商编码")
    private PartyRole supplier;


    @States(name = "提货方式", values = {
            @State(id = "selfMention", name = "自提"),
            @State(id = "delivery", name = "送货")
    })
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "carry_type", referencedColumnName = "id")
    private BusinessAttributeValue carryType;

    @Column(name = "purchase_Order", length = 64)
    @Comment("采购订单号")
    private String purchaseOrder;

    @Column(name = "aims_key", length = 64)
    @Comment("目标来源单据号码")
    private String aimsKey;

}
